package service

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"modulename/dao"
	"modulename/model/entity"
	"modulename/model/response"
	_ "net/http"
	"time"
)

// GetUserByName
// @tags user
// @Summary 通过姓名查询用户
// @Param name query string false "输入用户名"
// @Success 200 {object} entity.User "success"
// @Router /user/getByName [get]
func GetUserByName(ctx *gin.Context) {
	id := ctx.Query("name")
	user := dao.GetUserById(id)
	response.Success(ctx, user)
}

// AddUser
// @tags user
// @Summary 添加用户
// @Param user query entity.User false "输入用户信息"
// @Success 200 {object} response.Response "请求成功"
// @Router /user/add [post]
func AddUser(ctx *gin.Context) {
	var user entity.User
	// 解析 JSON
	var m map[string]interface{}
	data, _ := ctx.GetRawData()
	_ = json.Unmarshal(data, &m)
	// 将 JSON 转为对象
	user.Age = int(m["Age"].(float64))
	user.Name = m["Name"].(string)
	// layout 需要使用这个时间
	user.Birthday, _ = time.Parse("2006-01-02 15:04:05", m["Birthday"].(string))
	dao.AddUser(user)
	response.Success(ctx, "插入成功")
}
